{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f71b974b-b5ba-44a1-80e6-79cbe3aa6f84",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Second Quantized Hamiltonian"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "151e1188",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T15:14:02.285843Z",
     "iopub.status.busy": "2024-05-07T15:14:02.282963Z",
     "iopub.status.idle": "2024-05-07T15:14:05.330696Z",
     "shell.execute_reply": "2024-05-07T15:14:05.330041Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from classiq import construct_chemistry_model, execute, show, synthesize\n",
    "from classiq.applications.chemistry import (\n",
    "    ChemistryExecutionParameters,\n",
    "    FermionicOperator,\n",
    "    HamiltonianProblem,\n",
    "    HEAParameters,\n",
    "    SummedFermionicOperator,\n",
    ")\n",
    "from classiq.execution import OptimizerType"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a34b8008",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T15:14:05.333874Z",
     "iopub.status.busy": "2024-05-07T15:14:05.333266Z",
     "iopub.status.idle": "2024-05-07T15:14:05.339319Z",
     "shell.execute_reply": "2024-05-07T15:14:05.338667Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2 * a\u207a\u2080a\u2080 + \n",
      "0.3 * a\u2081a\u2081 + \n",
      "0.4 * a\u2082a\u2082 + \n",
      "0.5 * a\u2083a\u2083 + \n",
      "-0.1 * a\u207a\u2080a\u207a\u2081a\u2081a\u2080 + \n",
      "-0.3 * a\u207a\u2082a\u207a\u2083a\u2082a\u2083\n"
     ]
    }
   ],
   "source": [
    "hamiltonian = SummedFermionicOperator(\n",
    "    op_list=[\n",
    "        (FermionicOperator(op_list=[(\"+\", 0), (\"-\", 0)]), 0.2),\n",
    "        (FermionicOperator(op_list=[(\"-\", 1), (\"-\", 1)]), 0.3),\n",
    "        (FermionicOperator(op_list=[(\"-\", 2), (\"-\", 2)]), 0.4),\n",
    "        (FermionicOperator(op_list=[(\"-\", 3), (\"-\", 3)]), 0.5),\n",
    "        (FermionicOperator(op_list=[(\"+\", 0), (\"+\", 1), (\"-\", 1), (\"-\", 0)]), -0.1),\n",
    "        (FermionicOperator(op_list=[(\"+\", 2), (\"+\", 3), (\"-\", 2), (\"-\", 3)]), -0.3),\n",
    "    ]\n",
    ")\n",
    "ham_problem = HamiltonianProblem(hamiltonian=hamiltonian, num_particles=[1, 1])\n",
    "\n",
    "print(ham_problem.hamiltonian)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b37a0870-c254-4459-a2cc-840a6f8d4a4f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T15:14:05.341642Z",
     "iopub.status.busy": "2024-05-07T15:14:05.341283Z",
     "iopub.status.idle": "2024-05-07T15:14:05.350068Z",
     "shell.execute_reply": "2024-05-07T15:14:05.349441Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "hwea_params = HEAParameters(\n",
    "    num_qubits=4,\n",
    "    connectivity_map=[(0, 1), (1, 2), (2, 3)],\n",
    "    reps=3,\n",
    "    one_qubit_gates=[\"x\", \"ry\"],\n",
    "    two_qubit_gates=[\"cx\"],\n",
    ")\n",
    "\n",
    "qmod = construct_chemistry_model(\n",
    "    chemistry_problem=ham_problem,\n",
    "    use_hartree_fock=False,\n",
    "    ansatz_parameters=hwea_params,\n",
    "    execution_parameters=ChemistryExecutionParameters(\n",
    "        optimizer=OptimizerType.COBYLA,\n",
    "        max_iteration=100,\n",
    "        initial_point=None,\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2f01b380-3fb9-42fe-946d-9a868a6aa02f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T15:14:05.352232Z",
     "iopub.status.busy": "2024-05-07T15:14:05.352059Z",
     "iopub.status.idle": "2024-05-07T15:14:05.367059Z",
     "shell.execute_reply": "2024-05-07T15:14:05.366458Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import write_qmod\n",
    "\n",
    "write_qmod(qmod, name=\"second_quantized_hamiltonian\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7657b2e7-4b3d-4ac1-807c-665dfb05007f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T15:14:05.369638Z",
     "iopub.status.busy": "2024-05-07T15:14:05.369046Z",
     "iopub.status.idle": "2024-05-07T15:14:08.802073Z",
     "shell.execute_reply": "2024-05-07T15:14:08.801359Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/2806f7a1-c17c-4a05-9abb-d19786e25884?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "qprog = synthesize(qmod)\n",
    "show(qprog)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ccad338b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T15:14:08.804889Z",
     "iopub.status.busy": "2024-05-07T15:14:08.804325Z",
     "iopub.status.idle": "2024-05-07T15:14:13.717989Z",
     "shell.execute_reply": "2024-05-07T15:14:13.717206Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0006000000000000068"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = execute(qprog).result()\n",
    "chemistry_result = results[0].value\n",
    "chemistry_result.energy"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "a07aacdcc8a415e7643a2bc993226848ff70704ebef014f87460de9126b773d0"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
